package com.project.isaacwiki.service.impl;

import com.project.isaacwiki.dto.ItemComplete;
import com.project.isaacwiki.mapper.MapperItemOpera;
import com.project.isaacwiki.mapper.MapperQueryItem;
import com.project.isaacwiki.pojo.*;
import com.project.isaacwiki.service.ServiceItemOpera;
import com.project.isaacwiki.util.AliyunOSSOperator;
import com.project.isaacwiki.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ServiceItemOperaImpl implements ServiceItemOpera {

    @Autowired
    private MapperItemOpera mapperItemOpera;

    @Autowired
    private MapperQueryItem mapperQueryItem;

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @Override
    @Transactional
    public Result deleteItem(Integer id) throws Exception{

        mapperItemOpera.deleteSynergyById(id);

        mapperItemOpera.deleteItemPoolById(id);

        mapperItemOpera.deleteItemAttributesById(id);

        mapperItemOpera.deleteItemsById(id);

        ItemImgPath itemImgPath = mapperQueryItem.queryItemImgPathById(id);
        if (itemImgPath != null) {
            String url = itemImgPath.getPath();
            String fileName = url.substring(url.lastIndexOf("/") + 1);
            aliyunOSSOperator.delete(fileName, "item_image");
            mapperItemOpera.deleteItemImgPathById(id);
        }

        return Result.Success();
    }

    @Override
    public Result uploadItemImage(MultipartFile file) throws Exception {
        if (file == null || file.isEmpty()) {
            return Result.Error("上传失败-无文件");
        }
        return Result.Success(aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename(), "item_image"));
    }

    @Override
    @Transactional
    public Result createItem(ItemComplete itemComplete) throws Exception {

        Item item = itemComplete.getItem();
        if (item == null) {
            return Result.Error("创建失败-无道具信息");
        }

        Integer id = item.getId();
        if (mapperQueryItem.queryItemById(id) != null) {
            return Result.Error("创建失败-道具已存在");
        }

        LocalDateTime localDateTime = LocalDateTime.now();
        item.setCreateTime(localDateTime);
        item.setChangeTime(localDateTime);
        if (mapperItemOpera.createItem(item) == 0) {
            throw new Exception("创建失败-道具输入出错");
        }

        ItemsAttributes itemsAttributes = itemComplete.getItemsAttributes();
        if (itemsAttributes == null) {
            throw new Exception("创建失败-无道具属性信息");
        }
        if (mapperItemOpera.createItemAttributes(itemsAttributes) == 0) {
            throw new Exception("创建失败-道具属性输入出错");
        }

        List<Synergy> synergies = itemComplete.getSynergies();
        if (synergies != null && !synergies.isEmpty()) {
            if (mapperItemOpera.createSynergies(synergies) == 0) {
                throw new Exception("创建失败-协同效应输入出错");
            }
        }

        List<ItemPoolType> itemPoolTypes = itemComplete.getItemPoolTypes();
        if (itemPoolTypes != null && !itemPoolTypes.isEmpty()) {
            if (mapperItemOpera.createItemPool(itemComplete.getItem().getId(), itemPoolTypes) == 0) {
                throw new Exception("创建失败-物品池输入出错");
            }
        }

        ItemImgPath itemImgPath = itemComplete.getItemImgPath();
        if (itemImgPath != null) {
            if (mapperItemOpera.createItemImgPath(itemImgPath) == 0) {
                throw new Exception("创建失败-道具图片输入出错");
            }
        }

        return Result.Success();

    }

    @Override
    public Result updateItemImage(MultipartFile file) throws Exception {
        if (file == null || file.isEmpty()) {
            return Result.Success("no update");
        }
        return Result.Success(aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename(), "item_image"));
    }

    @Override
    @Transactional
    public Result updateItem(ItemComplete itemComplete) throws Exception {
        Item item = itemComplete.getItem();
        if (item == null) {
            return Result.Error("更新失败-无道具信息");
        }
        LocalDateTime localDateTime = LocalDateTime.now();
        item.setChangeTime(localDateTime);
        mapperItemOpera.updateItems(item);

        ItemsAttributes itemsAttributes = itemComplete.getItemsAttributes();
        if (itemsAttributes == null) {
            throw new Exception("更新失败-无道具属性信息");
        }
        mapperItemOpera.updateItemAttributes(itemsAttributes);

        List<Synergy> synergies = itemComplete.getSynergies();
        if (synergies != null && !synergies.isEmpty()) {
            mapperItemOpera.deleteSynergyById(item.getId());
            if (mapperItemOpera.createSynergies(synergies) == 0) {
                throw new Exception("更新失败-协同效应输入出错");
            }
        }

        List<ItemPoolType> itemPoolTypes = itemComplete.getItemPoolTypes();
        if (itemPoolTypes != null && !itemPoolTypes.isEmpty()) {
            mapperItemOpera.deleteItemPoolById(item.getId());
            if (mapperItemOpera.createItemPool(item.getId(), itemPoolTypes) == 0) {
                throw new Exception("更新失败-物品池输入出错");
            }
        }

        ItemImgPath itemImgPath = itemComplete.getItemImgPath();
        if (itemImgPath != null && !itemImgPath.getPath().equals("no update")) {
            ItemImgPath itemImgPathOld = mapperQueryItem.queryItemImgPathById(item.getId());
            if (itemImgPathOld != null) {
                String url = itemImgPathOld.getPath();
                String fileName = url.substring(url.lastIndexOf("/") + 1);
                aliyunOSSOperator.delete(fileName, "item_image");
            }
            if (mapperItemOpera.updateItemImgPath(itemImgPath) == 0) {
                throw new Exception("更新失败-道具图片输入出错");
            }
        }

        return Result.Success();
    }
}
